package com.lb.poitl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.reflect.FieldUtils;
import org.apache.poi.xwpf.usermodel.XWPFTable;

import com.deepoove.poi.data.TextRenderData;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import com.deepoove.poi.util.TableTools;

/**
 * @author zhangjun
 */
public class RowspanPolicy<T> extends LoopRowTableRenderPolicy {
    private int rowIndex;
    private String fieldName;

    public RowspanPolicy(boolean onSameLine) {
        this("[", "]", onSameLine, 0, "");
    }

    public RowspanPolicy(int rowIndex, String fieldName) {
        this("[", "]", false, rowIndex, fieldName);
    }

    public RowspanPolicy(String prefix, String suffix) {
        this(prefix, suffix, false, 0, "");
    }

    public RowspanPolicy(String prefix, String suffix, boolean onSameLine, int rowIndex, String fieldName) {
        super(prefix, suffix, onSameLine);
        this.rowIndex = rowIndex;
        this.fieldName = fieldName;
    }

    @Override
    protected void afterloop(XWPFTable table, Object data) {
        if (null == data) return;
        if (null == fieldName || "".equals(fieldName)) return;
        List<T> list = (List<T>) data;
        String curr = null;
        int count = 0;//重复数量
        for (int i = 0; i < list.size(); i++) {
            String str = getFieldValue(list.get(i), fieldName);
            if (curr == null) {
                curr = str;
                count = 1;
            } else if (curr.equals(str)) {
                ++count;
            } else {
                mergeCellsVertically(table, rowIndex, i, count);
                curr = str;
                count = 1;
            }
        }
        mergeCellsVertically(table, rowIndex, list, count);
    }

    /**
     * 合并行
     *
     * @param rowIndex 列
     * @param index    重复行下标
     * @param count    重复数量
     */
    static void mergeCellsVertically(XWPFTable table, int rowIndex, int index, int count) {
        if (count > 1) {
            TableTools.mergeCellsVertically(table, rowIndex, index - count + 1, index);
        }
    }

    static <T> void mergeCellsVertically(XWPFTable table, int rowIndex, List<T> list, int count) {
        if (count > 1) TableTools.mergeCellsVertically(table, rowIndex, list.size() - count + 1, list.size());
    }

    /**
     * 获取值，判断是重复
     *
     * @param target    数据源
     * @param fieldName 字段名
     * @return 值
     */
    public String getFieldValue(Object target, String fieldName) {
        try {
            if (target instanceof Map) {
                return ((Map<?, ?>) target).get(fieldName).toString();
            }

            Object HSSFCell = FieldUtils.readField(target, fieldName, true);
            if (HSSFCell instanceof String) {
                return (String) HSSFCell;
            }
            TextRenderData textRenderData = (TextRenderData) HSSFCell;
            return textRenderData.getText();
        } catch (IllegalAccessException e) {
            throw new RuntimeException("字段类型不匹配，请检查字段类型和实体类是否一致");
        }
    }
}